<?php

/**
 * 加载$mod
 */
function setRequest()
{
    global $_G;
    $mod = $_G["page"]["mod"];
    if ($mod == "") {
        printMessage("error", "无效的板块请求，请重试，或联系网站管理员");
    } else {
        $path = getModEntryPath($mod);
        if (! file_exists($path)) {
            printMessage("error", "无法找到目标模块 $mod 的入口函数，无效的请求，请联系网站管理员");
        } else {
            require_once $path;
        }
    }
}

/**
 * 获取模块入口文件的绝对路径
 *
 * @param string $mod
 * @return string path
 */
function getModEntryPath($mod)
{
    return getModulePath() . "/$mod/$mod" . ".entry.php";
}

function print_json_setting($mod)
{
    global $temple_settings;
    echo isset($temple_settings[$mod]) ? json_encode($temple_settings[$mod]) : "{}";
}

function direct_print_setting($mod, $setting)
{
    global $temple_settings;
    echo isset($temple_settings[$mod][$setting]) ? $temple_settings[$mod][$setting] : "";
}

function printMessage($head, $detial)
{
    global $messageHead;
    $messageHead = $head;

    global $messageDetial;
    $messageDetial = $detial;
    global $_G;
    $_G["page"]["mod"] = "information";
    setRequest();
}

function api_message($message, $id = -1,$type="error")
{
    if (! isAjaxRequest()) {
        printMessage($type, $message);
        die();
    } else
        returnMessage($id, $message);
    die();
}

function printHTMLValue($value)
{
    return "value=\"$value\"";
}

function setMod($mod = "home")
{
    global $_G;
    if ($mod == "") {
        $_G["page"]["mod"] = "home";
    }
    $_G["page"]["mod"] = $mod;
}

function printMod()
{
    global $_G;
    if ($_G["page"]["mod"] = "") {
        setTitle("");
    }
    echo $_G["page"]["mod"];
}

function setTitle($title = "")
{
    global $_G;

    if ($title == "") {
        global $_GCONFIG;
        $_G["page"]["title"] = $_GCONFIG["temple"]["defaultTitle"];
    }
    $_G["page"]["title"] = $title;
}

function printTitle()
{
    global $_G;
    if ($_G["page"]["title"] = "") {
        setTitle("");
    }
    echo $_G["page"]["title"];
}

/**
 * 加载模板
 */
function loadTemp($name)
{
    $filename = getRootPath() . "/temples/" . $name;
    if (file_exists($filename)) {
        require_once $filename;
        return true;
    }
    debug("模板未找到赶紧滚");
    return false;
}
?>